#!/bin/bash

######################################################
# file : 
# Author : Anil Kainikara (anilkumar911@gmail.com)
# 
#
# This code is licenced under the GPL.
#
######################################################

export day=
export month=
export year=

diarydb="/home/anil/.local/share/diary.db"
#tmp_folder="/media/store/workshop/vim_diary/"
tmp_folder="/tmp/__diary_"

mkdir -p $tmp_folder

DATE_Y2H=`date +"%Y-%m-%d-%H-%M"`

usage()
{
  echo "Usage : editdiary dd/mm/yyyy"
}

parsedate()
{
  date=$1
  if [[ -z $1 ]]
  then
    date=`date +%d/%m/%Y`;
  fi
  day=`echo $date | cut -d'/' -f1`
  month=`echo $date | cut -d'/' -f2`
  year=`echo $date | cut -d'/' -f3`
}

cleanup()
{
  rm -f ${tmp_folder}/diary${DATE_Y2H}.content
  #rm -f ${tmp_folder}/diary${DATE_Y2H}.content.new
}

main()
{
    parsedate $@
    endaction="create"
    #echo $day " " $month " " $year
    sqlite3 $diarydb "select content from entries where day=$day and  month=$month and year=$year;" > ${tmp_folder}/diary${DATE_Y2H}.content
    if [[ -s ${tmp_folder}/diary${DATE_Y2H}.content ]]
    then
       endaction="edit"
    fi
    echo  " n "${tmp_folder}/diary${DATE_Y2H}.content
}

main $@
